package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.purchase.RCInvoiceType;
import com.shrct.businessprocess.purchase.RCPurchaseIncomeRequest;
import com.shrct.businessprocess.purchase.RCPurchaseInvoiceItem;
import com.shrct.businessprocess.purchase.impl.RCPurchaseIncomeRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCPurchaseInvoiceItemProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCPURCHASEINVOICEITEM_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcpurchaseinvoiceitem")
public class RCPurchaseInvoiceItemImpl extends BaseModelObject implements RCPurchaseInvoiceItem {
  public RCPurchaseInvoiceItemImpl() {
    super();
  }
  
  public RCPurchaseInvoiceItemImpl(final RCPurchaseIncomeRequest incomeRequest) {
    super();
    this.incomeRequest = incomeRequest;
  }
  
  @JoinColumn(name = "incomeRequest_id")
  @ManyToOne(targetEntity = RCPurchaseIncomeRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCPurchaseIncomeRequest incomeRequest;
  
  public RCPurchaseIncomeRequest getIncomeRequest() {
    return this.incomeRequest;
  }
  
  public RCPurchaseInvoiceItem setIncomeRequest(final RCPurchaseIncomeRequest incomeRequest) {
    this.incomeRequest = incomeRequest;
    return this;			
    
  }
  
  private String productName;
  
  public String getProductName() {
    return this.productName;
  }
  
  public RCPurchaseInvoiceItem setProductName(final String productName) {
    this.productName = productName;
    return this;			
    
  }
  
  private String productSpec;
  
  public String getProductSpec() {
    return this.productSpec;
  }
  
  public RCPurchaseInvoiceItem setProductSpec(final String productSpec) {
    this.productSpec = productSpec;
    return this;			
    
  }
  
  private String unit;
  
  public String getUnit() {
    return this.unit;
  }
  
  public RCPurchaseInvoiceItem setUnit(final String unit) {
    this.unit = unit;
    return this;			
    
  }
  
  private Double unitPrice;
  
  public Double getUnitPrice() {
    return this.unitPrice;
  }
  
  public RCPurchaseInvoiceItem setUnitPrice(final Double unitPrice) {
    this.unitPrice = unitPrice;
    return this;			
    
  }
  
  private Double quantity;
  
  public Double getQuantity() {
    return this.quantity;
  }
  
  public RCPurchaseInvoiceItem setQuantity(final Double quantity) {
    this.quantity = quantity;
    return this;			
    
  }
  
  private RCInvoiceType invoiceType;
  
  public RCInvoiceType getInvoiceType() {
    return this.invoiceType;
  }
  
  public RCPurchaseInvoiceItem setInvoiceType(final RCInvoiceType invoiceType) {
    this.invoiceType = invoiceType;
    return this;			
    
  }
  
  public Double getSubTotal() {
    double _multiply = DoubleExtensions.operator_multiply(
      this.unitPrice, this.quantity);
    return Double.valueOf(_multiply);
  }
  
  public Double getActualIncome() {
    Double _subTotal = this.getSubTotal();
    Double _actualTax = this.getActualTax();
    double _minus = DoubleExtensions.operator_minus(_subTotal, _actualTax);
    return Double.valueOf(_minus);
  }
  
  public Double getActualTax() {
    double _xblockexpression = (double) 0;
    {
      double _xifexpression = (double) 0;
      boolean _equals = Objects.equal(this.invoiceType, RCInvoiceType.productType);
      if (_equals) {
        _xifexpression = 0.17;
      } else {
        _xifexpression = 0.06;
      }
      final Double taxRate = NumberExtensions.nullsafe(Double.valueOf(_xifexpression));
      Double _subTotal = this.getSubTotal();
      double _plus = (1 + (taxRate).doubleValue());
      double _divide = ((_subTotal).doubleValue() / _plus);
      double _multiply = (_divide * (taxRate).doubleValue());
      _xblockexpression = (_multiply);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public static RCPurchaseInvoiceItem create(final RCPurchaseIncomeRequest incomeRequest, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCPurchaseInvoiceItem rCPurchaseInvoiceItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseInvoiceItemImpl(
    	incomeRequest
    );
    objectFactory.create(rCPurchaseInvoiceItem);
    return rCPurchaseInvoiceItem;			
    
  }
  
  public static RCPurchaseInvoiceItem createRCPurchaseInvoiceItemImpl(final RCPurchaseIncomeRequest incomeRequest, final IObjectFactory objectFactory, final Procedure1<RCPurchaseInvoiceItem> init) {
    com.shrct.businessprocess.purchase.RCPurchaseInvoiceItem rCPurchaseInvoiceItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseInvoiceItemImpl(
    	incomeRequest
    );
    objectFactory.create(rCPurchaseInvoiceItem);
    init.apply(rCPurchaseInvoiceItem);
    
    return rCPurchaseInvoiceItem;			
    
  }
  
  public RCPurchaseInvoiceItemProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCPurchaseInvoiceItemProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCPurchaseInvoiceItemProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
